package com.bnz.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import com.bnz.service.impl.ClassesServiceImpl;
import com.bnz.service.impl.StudentServiceImpl;
import com.sun.org.apache.xml.internal.security.Init;
import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService;
    @Override
    public void init() {
        studentService = new StudentServiceImpl();
    }

    /**
     * 1. 根据不同的请求参数调用不同的处理方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.0 处理post请求的中文乱码
        req.setCharacterEncoding("UTF-8");
        //1.1 设置响应对象的编码类型
       // resp.setContentType("text/html;charset=utf8");
        //1.2 s到请求参数
        String cmd = req.getParameter("cmd");
        //1.3 根据参数的值来调用不同的方法
        if(StrUtil.isNotBlank(cmd)){
            if("list".equals(cmd)){         // 列表学生
                list(req,resp);
            }
            if("findByPage".equals(cmd)){  // 分页查询
                findByPage(req,resp);
            }
            if("search".equals(cmd)){       // 条件查询带分页
                search(req,resp);
            }
        }
    }


    /**
     * 2. 学生列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void list(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        //2.1 查询所有学后
        List<Student> students = studentService.findAll();
        //2.2 转换为json串
        String s = JSON.toJSONString(students);
        //2.3 输出到页面
        resp.getWriter().println(s);
        //2.4 刷新页面
        resp.getWriter().flush();

    }

    /**
     * 3. 分页查询
     * @param req
     * @param resp
     */
    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //3.1 得到分页查询的参数
        int page = new Integer(req.getParameter("page"));               // 当前页
        int pagesize = new Integer(req.getParameter("pagesize"));       // 每页大小
        //3.2 开始分页
        PageResult<Student> pr = studentService.findByPage(page,pagesize);
        //3.3 转换为json串
        String s = JSON.toJSONString(pr);
        //3.4 输出到页面
        resp.getWriter().println(s);
        //3.5 刷新页面
        resp.getWriter().flush();
    }

    //4. 条件查询带分页
    private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //4.1 得到分页查询的参数
        int page = new Integer(req.getParameter("page"));               // 当前页
        int pagesize = new Integer(req.getParameter("pagesize"));       // 每页大小
        //4.2 得到查询参数
        String stud = req.getParameter("stud");
        //4.3 转换为java对象StudentVo
        StudentVo studentVo = JSON.parseObject(stud, StudentVo.class);
        System.out.println("vo = " + studentVo);
        //4.4 开始条件查询带分页
        PageResult<Student> pr = studentService.search(page,pagesize,studentVo);
        //4.5 转换为json串
        String s = JSON.toJSONString(pr);
        //4.6 输出到页面
        resp.getWriter().println(s);
        //4.7 刷新页面
        resp.getWriter().flush();

    }
}
